package view;

import java.awt.Color;
import java.awt.Dimension;

import javax.swing.JComponent;
import javax.swing.JPanel;

import com.alee.laf.WebLookAndFeel;
import com.alee.laf.optionpane.WebOptionPane;
import com.alee.laf.progressbar.WebProgressBar;
import com.alee.laf.rootpane.WebDialog;
import com.alee.laf.rootpane.WebFrame;
import com.alee.laf.tabbedpane.WebTabbedPane;

import controller.Controls;

public class VisualCandoGUI {
    
    private static final Dimension FRAME_DIM = new Dimension(600, 750);
    
    private static final Dimension DB_FRAME_DIM = new Dimension(400, 400);
    
    private static final Dimension DIALOG_DIM = new Dimension(525, 650);
    
    private final Controls VisualCandoControls;
    
    private final WebFrame frame;
    
    private ResultsDialog resultsDialog;
    
    private ResultsDialog heatMapImageDialog;
    
    private ResultsDialog waitDialog;
    
    private WebTabbedPane resultsTabbedPane;
    
    private WebTabbedPane imageTabbedPane;
    
    private HeatMapPanel heatMapPanel;
    
    /**
     * Constructs main frame and sets controller reference.
     * 
     * @param controller Reference to controller.
     */
    public VisualCandoGUI(Controls VisualCandoControls) {
        this.VisualCandoControls = VisualCandoControls;
        this.frame = new WebFrame("VisualCando");
    }
    
    /**
     * Creates view displayed at application launch.
     */
    public void createInitialView() {
    	boolean decorateFrames = WebLookAndFeel.isDecorateFrames ();
        WebLookAndFeel.setDecorateFrames(true);
        this.frame.setIconImages(WebLookAndFeel.getImages());
        this.frame.setDefaultCloseOperation(WebFrame.EXIT_ON_CLOSE);
        this.frame.setPreferredSize(FRAME_DIM); 
        this.heatMapPanel = new HeatMapPanel(this.VisualCandoControls);
        this.frame.add(this.heatMapPanel);
        this.frame.pack();
        this.frame.setLocationRelativeTo(null);
        this.frame.setVisible(true);
        WebLookAndFeel.setDecorateFrames ( decorateFrames );        
    }
    
    public void createDatabaseGUIView(final JPanel databaseView) {
    	boolean decorateFrames = WebLookAndFeel.isDecorateFrames ();
        WebLookAndFeel.setDecorateFrames(true);
        this.frame.setIconImages(WebLookAndFeel.getImages());
        this.frame.setDefaultCloseOperation(WebFrame.EXIT_ON_CLOSE);
        this.frame.setPreferredSize(DB_FRAME_DIM); 
        this.frame.add(databaseView);
        this.frame.pack();
        this.frame.setLocationRelativeTo(null);
        this.frame.setVisible(true);
        WebLookAndFeel.setDecorateFrames ( decorateFrames );
    }
    
    public void launchNewFrame(final String title, final JComponent displayComponent) {
    	if (this.resultsDialog == null || !this.resultsDialog.isVisible()) {
    		this.resultsTabbedPane = new WebTabbedPane ();
    		resultsTabbedPane.setPreferredSize (DIALOG_DIM);
    		resultsTabbedPane.setTabPlacement (WebTabbedPane.LEFT);
    		this.resultsTabbedPane.add("Query 1", displayComponent);
    		boolean decorateFrames = WebLookAndFeel.isDecorateDialogs ();
    		WebLookAndFeel.setDecorateDialogs ( true );
    		// Opening dialog
    		this.resultsDialog = new ResultsDialog(title, this.frame, false);
    		this.resultsDialog.add(resultsTabbedPane);
    		//this.resultsDialog.setPreferredSize(FRAME_DIM);
    		this.resultsDialog.pack ();
    		this.resultsDialog.setLocationRelativeTo (displayComponent);
    		this.resultsDialog.setVisible ( true );

    		// Restoring frame decoration option
    		WebLookAndFeel.setDecorateDialogs ( decorateFrames );
    	} else {
    		this.resultsTabbedPane.add("Query " + (this.resultsTabbedPane.getTabCount() + 1), displayComponent);
    		this.resultsDialog.pack();
    	}
    }
    
    public void launchHeatMapImageDialog(final String title, final JComponent displayComponent) {
    	if (this.heatMapImageDialog == null || !this.heatMapImageDialog.isVisible()) {
    		this.imageTabbedPane = new WebTabbedPane ();
    		imageTabbedPane.setPreferredSize(DIALOG_DIM);
    		imageTabbedPane.setTabPlacement (WebTabbedPane.TOP);
    		this.imageTabbedPane.setBackground(Color.WHITE);
    		this.imageTabbedPane.add("Image 1", displayComponent);
    		boolean decorateFrames = WebLookAndFeel.isDecorateDialogs ();
    		WebLookAndFeel.setDecorateDialogs ( true );
    		// Opening dialog
    		this.heatMapImageDialog = new ResultsDialog(title, this.frame, false);
    		this.heatMapImageDialog.add(imageTabbedPane);
    		//this.resultsDialog.setPreferredSize(FRAME_DIM);
    		this.heatMapImageDialog.pack ();
    		this.heatMapImageDialog.setLocationRelativeTo (displayComponent);
    		this.heatMapImageDialog.setVisible ( true );

    		// Restoring frame decoration option
    		WebLookAndFeel.setDecorateDialogs ( decorateFrames );
    	} else {
    		this.imageTabbedPane.add("Image " + (this.imageTabbedPane.getTabCount() + 1), displayComponent);
    		this.heatMapImageDialog.pack();
    	}
    }
    
    public void launchWaitDialog() {
        WebProgressBar progressBar = new WebProgressBar();
        progressBar.setIndeterminate (true);
        progressBar.setStringPainted (true);
        progressBar.setString ( "Please wait..." );
        
		boolean decorateFrames = WebLookAndFeel.isDecorateDialogs ();
		WebLookAndFeel.setDecorateDialogs (true);      
        this.waitDialog = new ResultsDialog("Query in Progress", this.frame, false);
        this.waitDialog.add(progressBar);
        this.waitDialog.pack();
        this.waitDialog.setLocationRelativeTo(this.frame);
        this.waitDialog.setVisible(true);
        
		// Restoring frame decoration option
		WebLookAndFeel.setDecorateDialogs ( decorateFrames );
    }
    
    public void closeWaitDialog() {
    	this.waitDialog.dispose();
    }
    
    public WebFrame getVisualCandoFrame() {
    	return this.frame;
    }
    
    public void updateView() {
    	this.frame.pack();
    }
    
    public void displayErrorMessage(final String message) {
    	WebOptionPane.showMessageDialog (this.frame, message, "Error", WebOptionPane.ERROR_MESSAGE );
    }
    
	private class ResultsDialog extends WebDialog {
		
		private static final long serialVersionUID = -5576544451846258649L;

		public ResultsDialog(final String title, final WebFrame owner, final boolean modalBool) {
			super(owner, title);
			setIconImages(WebLookAndFeel.getImages());
			setDefaultCloseOperation(WebDialog.DISPOSE_ON_CLOSE);
			setResizable(true);
			setModal(modalBool);
		}
    }
	
	public Color getLowColor() {
		return this.heatMapPanel.getLowColor();
	}
	
	public Color getHighColor() {
		return this.heatMapPanel.getHighColor();
	}
}
